<?php

namespace app\api\controller\order;

use app\api\controller\Api;
use app\common\order\model\OrderTransferLog;
use app\common\order\model\OrderUser;
use think\db\exception\DbException;

class User extends Api
{
    // 转单 15=销售
    public function transfer()
    {
        $post = $this->request->post();
        $user = $this->request->user;
        if (empty($post['order_id']) || empty($post['user_id'])) {
            return error([], "参数错误");
        }

        try {
            $where = [];
            $where[] = ['id', 'in', $post['order_id']];
            \app\common\order\model\Order::update(['user_id' => $post['user_id']], $where);
            $notices = [];

            $data = [];
            $now = time();
            foreach ($post['order_id'] as $orderId) {
                // 转单数据
                $row = [];
                $row['operator_id'] = $user['id'];
                $row['user_id'] = $post['user_id'];
                $row['order_id'] = $orderId;
                $row['create_time'] = $now;
                array_push($data, $row);

                // 通知数据
                $notice = [];
                $notice['from_user_id'] = $user['id'];
                $notice['user_id'] = $post['user_id'];
                $notice['type'] = 2;
                $notice['content'] = '您有一笔转交订单，请注意查看';
                $notice['options'] = [
                    'order_id' => $orderId,
                ];
                array_push($notices, $notice);
            }

            // 转单日志
            $logModel = new OrderTransferLog();
            $logModel->insertAll($data);

            // 记录通知
            $noticeModel = new \app\common\system\model\Notice();
            $noticeModel->saveAll($notices);
        } catch (DbException $e) {
            return error([], $e->getMessage());
        }

        return success([], "订单转交成功");
    }

    // 分配策划 17 = 策划
    public function planner()
    {
        $post = $this->request->post();
        $user = $this->request->user;
        if (empty($post['order_id']) || empty($post['user_id'])) {
            return error([], "参数错误");
        }

        $where = [];
        $where['user_id'] = $post['user_id'];
        $where['order_id'] = $post['order_id'];
        $model = new OrderUser();
        $row = $model->where($where)->find();
        if (!empty($row)) {
            return error([], "请勿重复分配");
        }

        // 分配策划数据
        $data = [];
        $data['operator_id'] = $user['id'];
        $data['user_id'] = $post['user_id'];
        $data['order_id'] = $post['order_id'];
        $data['role_id'] = 17;

        // 通知数据
        $notice = [];
        $notice['from_user_id'] = $user['id'];
        $notice['user_id'] = $post['user_id'];
        $notice['type'] = 3;
        $notice['content'] = '您有一笔策划订单，请注意查看';
        $notice['options'] = [
            'order_id' => $post['order_id'],
        ];

        try {
            $model->save($data);
            $updateWhere = [];
            $updateWhere[] = ['id', '=', $post['order_id']];
            \app\common\order\model\Order::update(['assign_status' => 1], $updateWhere);

            // 记录通知
            $noticeModel = new \app\common\system\model\Notice();
            $noticeModel->save($notice);
        } catch (DbException $e) {
            return error([], $e->getMessage());
        }

        return success([], "分配策划成功");
    }
}
